'use strict';

const { Model } = require('sequelize');

module.exports = (sequelize, DataTypes) => {
  class CustomerData extends Model {
    static associate(models) {
      // 定义关联
      CustomerData.belongsTo(models.User, { foreignKey: 'assignedUserId', as: 'assignedUser' });
    }
  }
  
  CustomerData.init({
    assignedUserId: {
      type: DataTypes.INTEGER,
      allowNull: true,
      references: {
        model: 'users',
        key: 'id'
      }
    },
    name: {
      type: DataTypes.STRING,
      allowNull: false
    },
    phone: {
      type: DataTypes.STRING,
      allowNull: true
    },
    email: {
      type: DataTypes.STRING,
      allowNull: true
    },
    address: {
      type: DataTypes.TEXT,
      allowNull: true
    },
    status: {
      type: DataTypes.ENUM('active', 'inactive', 'pending', 'archived'),
      defaultValue: 'active'
    },
    customerType: {
      type: DataTypes.STRING,
      allowNull: true
    },
    source: {
      type: DataTypes.STRING,
      allowNull: true
    },
    lastInteraction: {
      type: DataTypes.DATE,
      allowNull: true
    },
    notes: {
      type: DataTypes.TEXT,
      allowNull: true
    },
    tags: {
      type: DataTypes.JSON,
      allowNull: true
    },
    metadata: {
      type: DataTypes.JSON,
      allowNull: true
    }
  }, {
    sequelize,
    modelName: 'CustomerData',
    tableName: 'customer_data',
    timestamps: true
  });
  
  return CustomerData;
}; 